/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TaBox.client;

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import tabox.server.Protocol;

/**
 *
 * @author timo
 */
public class ClientHandler extends Thread{
   
        Socket socket;
        OutputStream out;
        InputStream in;
        byte[] intext,outtext;
        FileChecker fc;

        public ClientHandler(Socket socket){
            this.socket = socket;
            fc = new FileChecker();
        }
        
        @Override
        public void run(){
        
            
            
            try {
                out = socket.getOutputStream();
                in = socket.getInputStream();
                while(in.read(intext) > 0){ 
                    
                    
                    
                    /*else if (intext.toString().equals("SYNCHRONIZING")){
                        receiveFile();                       
                    }*/
                    
                }
            } catch (IOException ex) {
                Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        }
    
        public void sendMeta(){
            
            
            try {
                out.write(("SYNC").getBytes());
                while (in.read(intext) < 1 && !(intext.toString().equals("DISCONNECT"))) {
                    System.out.println("Server: " + intext);
                    if (intext.toString().equals("READY")){
                        fc.queueMessage(out,(new File(fc.path)).listFiles());
                        out.write(("FINISH").getBytes());
                        break;
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
        
        public void receiveFile(){
            
            
            try {
                out.write("READY".getBytes());
                while (in.read(intext) < 1 && !(intext.toString().equals("FINISH"))) {
                    in.read(intext);
                    String path = intext.toString();
                    in.read(intext);
                    fc.writeFile(path, intext);
                    out.write("RECEIVED".getBytes());
                }
            } catch (IOException ex) {
                Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
        
        
    
    
    
    
}
    

